package com.example.designpatternproject.observer;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.base.BaseActivity;
import com.example.designpatternproject.R;
import com.example.designpatternproject.databinding.ActivityObserverBinding;
import com.example.ext.ActivityExtKt;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName: ObserverActivity
 * @Author: 史大拿
 * @CreateDate: 10/26/22$ 1:32 PM$
 * TODO 观察者设计模式
 * <p>
 * 观察者
 */
public class ObserverActivity extends BaseActivity<ActivityObserverBinding> implements Observer<String> {
    RecyclerViewAdapter adapter;

    @Override
    public void initView(Bundle savedInstanceState) {

        Objects.requireNonNull(getSupportActionBar()).setTitle("观察者");

        /*
         * 作者:史大拿
         * 创建时间: 10/26/22 2:57 PM
         * TODO 利用单例模式 + 观察者模式 实现两个页面数据及时相应
         * A : 观察者(ObserverActivity)
         * B : 被观察者(ObservableActivity)
         *
         * A页面监听数据变化， B页面改变数据，
         * 当B页面改变数据后，A页面需要立刻响应
         */
        /*
         * TODO 绑定
         */
        ObservableManager.getInstance().register(this);

        initRecycler();

        binding.btn.setOnClickListener(v -> ActivityExtKt.jumpActivity(ObserverActivity.this, v, ObservableActivity.class));
    }

    private void initRecycler() {
        adapter = new RecyclerViewAdapter();
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        binding.recyclerView.setAdapter(adapter);
    }

    @Override
    public void update(String value) {
        Toast.makeText(getApplicationContext(), "回调了:" + value, Toast.LENGTH_SHORT).show();
        adapter.addData(value);
    }


    @Override
    protected void onDestroy() {
        /*
         * TODO 解绑
         */
        ObservableManager.getInstance().unregister(this);
        super.onDestroy();
    }

    private static class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

        public List<String> mData;

        RecyclerViewAdapter() {
            mData = new ArrayList<>();
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_text, parent, false));
        }
        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            holder.tv.setText(mData.get(position));
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }

        public void addData(String value) {
            mData.add(value);
            notifyDataSetChanged();
        }

        private static class ViewHolder extends RecyclerView.ViewHolder {

            private final TextView tv;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                tv = itemView.findViewById(R.id.tv);
            }
        }
    }
}
